<?
// +----------------------------------------------------------------------
// | 分销商价格配置
// +----------------------------------------------------------------------
// | Author: TongBC <50249101@qq.com>
// +----------------------------------------------------------------------
// | Date  : 2015/01/07
// +----------------------------------------------------------------------
include_once("/var/www/html/new/com.inc.php");
include_once("/var/www/html/new/d/common/header.html");
include_once("/var/www/html/new/d/common/page.php");
include_once("/var/www/html/new/d/common/left.html");
$ac=$GLOBALS['_ac'];
$pw=$GLOBALS['_pw'];
$s = new SoapClient(null,array("location" => "http://localhost/open/openService/pft_insideMX.php","uri" => "www.16u.com?ac_16u=$ac|pw_16u=$pw|auth_16u=true"));

$sid=$_SESSION['sid'];// 会员 ID
$did=$_REQUEST["did"];// 分销商 ID
$currentPage=$_REQUEST["currentPage"] ? $_REQUEST["currentPage"]:1;
$pageSize=$_REQUEST['pageSize'] ? $_REQUEST["pageSize"]:15;

// 分销商名称
$sel="select dname,account from pft_member where id=$did limit 1";
$GLOBALS['le']->query($sel);
$GLOBALS['le']->fetch_assoc();
$dname = $GLOBALS['le']->f("dname");
$account = $GLOBALS['le']->f("account");




// 分销商可销售会员产品
$sale_pids = array();
$sql="select pids from pft_product_sale_list where aid=$sid and fid=$did limit 1";
$GLOBALS['le']->query($sql);
$GLOBALS['le']->fetch_assoc();
$pids = $GLOBALS['le']->f('pids');
if($pids && $pids!='A'){ // 指定个别可销售
    $sale_pids = explode(',',$pids);
    $f_dprice = array();// 个别配置的产品
    $sql="select tid,dprice from `uu_priceset` where pid=$did and tid in ($pids)";
    $GLOBALS['le']->query($sql);
    while($row=$GLOBALS['le']->fetch_assoc()){
        $f_dprice[$row['tid']] = $row['dprice']/100;
    }
}


// 会员自己供应产品
$apply_pids = array();
$prod = array();
$sql="select p.id,p.p_name,l.attribute,l.p_type from uu_products p left join uu_land l on p.contact_id=l.id where p.apply_did=$sid and p.p_status=0 and p.apply_limit=1 order by p.p_name desc";
$GLOBALS['le']->query($sql);
$account = $_SESSION['saccount'];
while($row=$GLOBALS['le']->fetch_assoc()){
    
    $jsprice = getPrice($s,$row['id'],$sid,$account);
    if($jsprice==-1) continue;
	if($row['p_type']=='F'){// 套票，子票线所有的分销商都不能转
		$pack_pids = json_decode($row['attribute'],true);
		foreach($pack_pids as $k=>$ct){
			$sql="select aids from pft_p_apply_evolute where pid={$ct['pid']} and fid=$sid and sid={$ct['aid']} limit 1";
			$GLOBALS['le']->query($sql);
			$GLOBALS['le']->fetch_assoc();
			$aids = $GLOBALS['le']->f('aids');
			if($aids && in_array($did,explode(',',$aids))) continue;// 套票子票有该分销商转发
		}
	}
	
    $apply_pids[$row['id']] = $row['id'];
    $prod[$row['id']] = array(
        'id'=>$row['id'],
        'p_name'=>$row['p_name'],
        'fp'=>$jsprice/100,
        'disabled'=>'disabled="true"',
        'other_f'=>0,
    );
    
    if(in_array($row['id'],$sale_pids)){
        $prod[$row['id']]['checked'] = 'checked="checked"';
        $prod[$row['id']]['disabled'] = '';
        $prod[$row['id']]['pri'] = $f_dprice[$row['id']];
    }
}


function getPrice($s,$pid,$aid,$account,$date=''){
    if($date=='') $date = date('Y-m-d');
    $xml = $s->Dynamic_Price_And_Storage($account,$pid,$date,1,0,0,$aid);
    if($xml==101 || $xml==1065 || $xml==103) return -1;
    if($xml==105) $xml = $s->Dynamic_Price_And_Storage($account,$pid,$date,2,0,0,$aid);
    if($xml==105) return -1;
    $object = simplexml_load_string($xml);
    return (int)$object->Rec->UUsprice;
}

?>
<style type="text/css">
button{cursor: pointer;}
.wrap{width:80%;float:right;}
table{width:100%; line-height:23px;margin:10px 0; border-color:#d5dce2;}
th,td{ text-align:center; border-color:#d5dce2; line-height:30px;}
th{background:#f2f9ff;}
.pri{width:109px;height:22px;text-align:center;}
.fenyeiWrap{width:85%;}
#tplid{display:none;}
.addcp {background:url("http://www.12301.cc/images/btn23_two1.png") no-repeat scroll left top;display: inline-block;height: 23px;padding-left: 8px;text-decoration: none;}
.addcp span {background:url("http://www.12301.cc/images/btn23_two1.png") no-repeat scroll right top;color: #FFFFFF;display: inline-block;font-size: 12px;font-weight: bold;height: 23px;line-height: 23px;padding-right: 8px;}
.ty{background:#f0851d; border:1px solid #a9550f; height:25px;width:75px; color:#fff; padding-bottom:2px;  padding-bottom:0px\9; }
.tpllist{float:right;line-height: 25px;}
.txtlf{ text-align:left; padding-left:15px;}
.dname-show{font-size:14px;font-weight:bold;color:#2A98DA;margin-right:10px;vertical-align: -2px;}
</style>
<script type="text/javascript">
function ty(){
	if($("#tplid").css("display")=="none"){
		$.getJSON("call/hasTpl.php",function(tpl){
			if(tpl){
				$("#tplid").show();
				var now=$("#now").val();
				for(var i=0;i<tpl.length;i++){
					$("#tplid").append("<option "+(tpl[i]['id']==now?"selected":"")+" value='"+tpl[i]['id']+"'>"+tpl[i]['title']+"</option>");
				}
			}
			else{
				if(confirm('检测到您尚未创建过模板,是否创建?')){
				//	window.open('addtpl.html','_blank');
					window.location='addtpl.html';
				}
			}
		})
	}
	else{
		if($("#tplid").val())$("#bind").val(1);
		document.forms[1].submit();
	}
}
function xzmb(){
	var t=$('#tplid').val();
	if(t>0){
		$("#bind").val(0);
		$.getJSON("call/getTpl.php",{tplid:t},function(tpl){
			$(":checkbox").each(function(){
				var ckbox=$(this);
				if(ckbox.val()!="all"){
					if(!isNaN(tpl[ckbox.val()])){
						ckbox.attr("checked",true).parent().parent().find(":text").attr("disabled",false).val(($("#stype").length && $("#stype").attr("checked") ? 0 :Number(ckbox.parent().parent().children().eq(2).html()))+Number(tpl[ckbox.val()]));
					}
					else{
						ckbox.attr("checked",false).parent().parent().find(":text").attr("disabled",true);
					}
				}
			});
		});
	}
	else if(t==-1){
		window.location='addtpl.html';
	}
}
function chPri(){
	$("#bind").val(0);
	if($("#tplid").val()){
		if(confirm('是否将该分销商与所选模板绑定,绑定后可通过修改模板修改该分销商的配置?')){
			$("#bind").val(1);
		}
	}
	document.forms[1].submit();
}
function changeDate(object){
    var $tr = $(object).parent().parent();
    $tr.find(':text').attr('disabled',!object.checked);
    $tr.find('.timely').attr('disabled',!object.checked);
}
</script>
<div class="wrap">
<iframe name="ifr" style="display:none;"></iframe>
<form action="priceset_dt.php" target="ifr">
<span class="dname-show"><?=$dname?></span>
<select name="tplid" id="tplid" onchange="xzmb()">
    <option value="">-请选择模板-</option>
    <option value="-1">--新增模板--</option>
</select>
<button type="button" onclick="ty()" class="ty">套用模板</button>
<font color="red">您可以选择套用模板来快速配置结算价。</font>
<a class="tpllist" onclick="history.go(-1)" href="javascript:void(0);">返回>></a>
<input type="hidden" name="did" value="<?=$did?>" />
<input type="hidden" name="bind" id="bind" value="<?=($tplid?1:0)?>" />
<input type="hidden" id="now" value="<?=$tplid?>" />
<table border="1">
<tr><th>购买权限</th><th>产品名称</th><th>供货价</th><th>结算价</th></tr>
<?foreach($prod as $p):?>
<tr>
<input type="hidden" name="others_f[<?=$p['id']?>]" value="<?=$p['other_f']?>" />
<input type="hidden" name="exi_pids[<?=$p['id']?>]" value="<?=$p['id']?>"/>
<input type="hidden" name="src_pris[<?=$p['id']?>]" class="timely" value="<?=$p['fp']?>" <?=$p['disabled']?>/>
<td><input type="checkbox" name="set_pids[]" <?=$p['checked']?> value="<?=$p['id']?>" onchange="changeDate(this)"/></td>
<td class="txtlf"><?=$p['p_name']?></td>
<td><?=$p['fp']?></td>
<td><input type="text" class="pri" <?=$p['disabled']?> name="set_pris[<?=$p['id']?>]" value="<?=$p['pri']+$p['fp']?>" /></td>
</tr>
<?endforeach;?>
</table>
<?//page($total,$currentPage,$pageSize);?>
<a class="addcp" onclick="chPri()" href="javascript:void(0);"><span>保存价格</span></a>
</form>
</div>
<?
include("common/footer.html");?>